package com.czk.ssachatprompt.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Objects;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-10-15
 * @Description:
 * @Version: 1.0
 */
@RestController
public class promptController {
    @Resource(name = "deepseekChatClient")
    private ChatClient deepeekChatClient;
    @Resource(name = "qwenChatClient")
    private ChatClient qwenChatClient;
    @Resource(name = "deepseek")
    private ChatModel deepeekChatModel;
    @Resource(name = "qwen")
    private ChatModel qwenChatModel;


    /**
     * chatClient获取大模型的对话
     *
     * @param question
     * @return
     */
    @GetMapping(value = "/prompt/chat-client")
    public String getAssistantMessage(@RequestParam(name = "question", defaultValue = "洛杉矶湖人") String question) {
        // 拿到大模型回复的消息
        AssistantMessage assistantMessage = Objects.requireNonNull(qwenChatClient.prompt()
                        .system("你是一个篮球助手，你只回答篮球相关问题，其他无关篮球的问题只需要回复下面句子：Lets Go Lakers")
                        .user(question)
                        .call()
                        .chatResponse())
                .getResult()
                .getOutput();
        return assistantMessage.getText();
    }

    /**
     * chatClient + prompt
     *
     * @param question
     * @return
     */
    @GetMapping(value = "/prompt/chat")
    public Flux<String> chat(@RequestParam(name = "question", defaultValue = "洛杉矶湖人") String question) {
        return deepeekChatClient.prompt()
                .system("你是一个篮球助手，你只回答篮球相关问题，其他无关篮球的问题只需要回复下面句子：Lets Go Lakers")
                .user(question)
                .stream()
                .content();
    }

    /**
     * chatModel + prompt
     *
     * @param question
     * @return 返回原始的对话信息，没有对内容进行提取
     */
    @GetMapping(value = "/prompt/chat-model")
    public Flux<ChatResponse> chatByModel(@RequestParam(name = "question", defaultValue = "洛杉矶湖人") String question) {
        SystemMessage systemMessage = new SystemMessage("你是一个讲故事的助手，每个故事的字数控制在300字左右");
        UserMessage userMessage = new UserMessage(question);
        Prompt prompt = new Prompt(systemMessage, userMessage);
        return deepeekChatModel.stream(prompt);


    }

    /**
     * 提取对话内容进行组装返回
     *
     * @param question
     * @return
     */
    @GetMapping(value = "/prompt/chat-model/text")
    public Flux<String> chatByModelText(@RequestParam(name = "question", defaultValue = "孙悟空") String question) {
        SystemMessage systemMessage = new SystemMessage("你是一个讲故事的助手，每个故事的字数控制在300字左右");
        UserMessage userMessage = new UserMessage(question);
        Prompt prompt = new Prompt(systemMessage, userMessage);
        return qwenChatModel.stream(prompt)
                .mapNotNull(response -> response.getResults().getFirst().getOutput().getText());


    }

    /**
     * 调用工具获取信息
     *
     * @param city
     * @return
     */
    @GetMapping(value = "/prompt/chat-client/tool")
    public String chatByClientTool(@RequestParam(name = "city", defaultValue = "成都") String city) {
        String result = deepeekChatClient.prompt()
                .user(city + "未来三天的天气如何")
                .call()
                .chatResponse()
                .getResult()
                .getOutput()
                .getText();
        // 工具消息
        ToolResponseMessage toolResponseMessage = new ToolResponseMessage(List.of(
                new ToolResponseMessage.ToolResponse("id", "获得天气", city)
        ));
        return result + toolResponseMessage.getText();
    }

}
